from .base_models import BaseModel
from django.db import models
from base.models import App


#销项发票配置
class SaleInvoiceConfig(BaseModel):
    app = models.ForeignKey(App, on_delete=models.CASCADE, verbose_name='所属应用')
    DZFP_API_TYPE = ( 
        (0, '航天接口'),
        (1, '诺诺接口'),#不再会有客户废弃
        (2, '诺诺税通接口'),
        (3, '乐企接口'),
    )
    dzfp_api_type = models.IntegerField(choices=DZFP_API_TYPE, verbose_name='电子发票接口类型', default=0)
    NSRSBH = models.CharField(max_length=20, default='', verbose_name='销项发票纳税人识别号',db_index=True)
    XHFMC = models.CharField(max_length=200, default='', verbose_name='销方名称')
    XHF_DZ = models.CharField(max_length=200, default='', verbose_name='销方地址')
    XHF_DH = models.CharField(max_length=50, default='', verbose_name='销方电话')
    XHF_YHZH = models.CharField(max_length=50, default='', verbose_name='销方银行账号')
    XHF_YHMC = models.CharField(max_length=200, default='', verbose_name='销方银行名称')

    KPY = models.CharField(max_length=50, default='', verbose_name='开票员')
    SKY = models.CharField(max_length=50, default='', verbose_name='收款员')
    FHR = models.CharField(max_length=50, default='', verbose_name='复核人')
    
    #电子发票-航天接口配置
    authorizationCode = models.CharField(max_length=100, null=True) #授权码
    userName = models.CharField(max_length=100, null=True) #电商平台编码
    passWord = models.CharField(max_length=100, null=True) #电商平台密码
    des3Key = models.CharField(max_length=100, null=True) #3des密钥
    HANGTIAN_URL = models.TextField(null=True) #航天服务地址

    #20230425诺税通接口配置
    nst_url = models.TextField(null=True) #服务地址
    nst_appKey = models.CharField(max_length=100, null=True) #
    nst_appSecret = models.CharField(max_length=100, null=True) #
    nst_accessToken = models.CharField(max_length=100, null=True) #
    #乐企业接口配置
    leq_url = models.TextField(null=True) #服务地址
    leq_smKey = models.CharField(max_length=50, null=True,verbose_name='国秘4密钥') #
    
    leq_appKey = models.CharField(max_length=100, null=True) #
    leq_ptbh = models.CharField(max_length=50, null=True,verbose_name='平台编码') #
    leq_jrdwptbh = models.CharField(max_length=50, null=True,verbose_name='直连单位ID') #
    leq_sydwptbh = models.CharField(max_length=50, null=True,verbose_name='使用单位ID') #
    leq_ssjswjgdm = models.CharField(max_length=50, null=True,verbose_name='所属税务机关代码') #
    leq_qydm = models.CharField(max_length=20, null=True,verbose_name='区域代码') #
    leq_ip = models.CharField(max_length=20, null=True,verbose_name='乐企出口IP地址') #
    leq_mac = models.CharField(max_length=50, null=True,verbose_name='乐企出口MAC地址') #
    leq_sqed = models.DecimalField(max_digits=18, decimal_places=2, verbose_name='发票额度', null=True)
    class Meta:
        verbose_name = '销项发票配置'
        verbose_name_plural = verbose_name

#发票额度
class InvoiceLimit(BaseModel):
    app = models.ForeignKey(App, on_delete=models.CASCADE, verbose_name='所属应用')
    saleInvoiceConfig = models.ForeignKey(SaleInvoiceConfig, on_delete=models.CASCADE, verbose_name='销项发票配置')
    NSRSBH = models.CharField(max_length=20, default='', verbose_name='纳税人识别号',db_index=True)
    syqjq = models.DateField(verbose_name='发票额度使用区间起',db_index=True)
    syqjz = models.DateField(verbose_name='发票额度使用区间止',db_index=True)
    limit_amount = models.DecimalField(max_digits=18, decimal_places=2, verbose_name='发票额度')

class Invoice(BaseModel):
    app = models.ForeignKey(App, on_delete=models.CASCADE, verbose_name='所属应用',db_index=True)
    saleInvoiceConfig = models.ForeignKey(SaleInvoiceConfig, on_delete=models.CASCADE, verbose_name='销项发票配置')
    SOURCE_CHOICES = (
        (0, '手动提交'),
        (1, '开票接口'),
        (2, '订单转换'),
        (3, 'Excel导入')
    )
    source = models.IntegerField(default=0, verbose_name='来源',choices=SOURCE_CHOICES)    
    statusQingqiu = models.IntegerField(default=0, verbose_name='请求状态',db_index=True)#0-未请求，1-已请求，3-请求失败
    KPSQ_STATUS = ( 
        (0, '待开具'),
        (1, '开具中'),
        (2, '开具成功'),
        (3, '开具失败'),
    )
    status = models.IntegerField(choices=KPSQ_STATUS, default=0, verbose_name='开票申请状态',db_index=True)
    INFO_KIND= ( 
        (0, '纸质增值税专用发票'),
        (1, u'废旧物资发票'),
        (2, '纸质增值税普通发票'),
        (11, '货运发票'),
        (41, '卷票普通发票'),
        (51, '电子增值税普通发票'),
        (61, '全电专票'),
        (62, '全电普票'),
        (-1, '未定义'),
    )
    InfoKind = models.IntegerField(default=0,choices=INFO_KIND, verbose_name='发票种类')
    KPLX_CHOICES = ( 
        (1, '正票'),
        (2, '红票'),
    ) 
    KPLX = models.IntegerField(choices=KPLX_CHOICES, default=1, verbose_name='开票类型')#1 正票 2 红票 
    
    upload_shuiju_status = models.IntegerField(default=0, verbose_name='上传税局状态',db_index=True)#0-未上传，1-已经请求等待回复，2-上传成功，3-上传失败
    kaipiao_lock = models.IntegerField(default=0) #开票锁，0-未锁定，1-已锁定
    fail_cnt = models.IntegerField(default=0) #失败次数
    fail_reason = models.TextField(null=True, blank=True, verbose_name='失败原因')
    fpkjApplyDate = models.DateTimeField(null=True, blank=True, verbose_name='发票申请日期')
    fpkjSuccessDate = models.DateTimeField(null=True, blank=True, verbose_name='发票成功日期')
    FPQQLSH = models.CharField(max_length=50, null=True, blank=True, verbose_name='请求流水号')
    FPQQRQ = models.DateTimeField(null=True, blank=True, verbose_name='发票请求日期')
    XHF_NSRSBH = models.CharField(max_length=20, default='', verbose_name='销项发票纳税人识别号',db_index=True)
    XHFMC = models.CharField(max_length=200, default='', verbose_name='销方名称')
    XHF_DZ = models.CharField(max_length=200, default='', verbose_name='销方地址')
    XHF_DH = models.CharField(max_length=50, default='', verbose_name='销方电话')
    XHF_YHZH = models.CharField(max_length=50, default='', verbose_name='销方银行账号')
    XHF_YHMC = models.CharField(max_length=200, default='', verbose_name='销方银行名称')

    GHFMC = models.CharField(max_length=200, default='', verbose_name='购方名称')
    GHF_NSRSBH = models.CharField(max_length=26, default='', verbose_name='购方纳税人识别号',db_index=True)
    GHF_DZ = models.CharField(max_length=200, default='', verbose_name='购方地址')
    GHF_SJ = models.CharField(max_length=50, default='', verbose_name='购方电话')
    GHF_EMAIL = models.CharField(max_length=50, default='', verbose_name='购方邮箱')
    GHF_YHZH = models.CharField(max_length=50, default='', verbose_name='购方银行账号')
    GHF_YHMC = models.CharField(max_length=200, default='', verbose_name='购方银行名称')
    gmfzrrbz = models.CharField(max_length=2, verbose_name='购买方自然人标志',default='N')

    isAuto2Kpsq = models.IntegerField(default=0, verbose_name='是否自动请求开票')

    KPY = models.CharField(max_length=50, default='', verbose_name='开票员')
    SKY = models.CharField(max_length=50, default='', verbose_name='收款员')
    FHR = models.CharField(max_length=50, default='', verbose_name='复核人')
    FP_DM = models.CharField(max_length=50, null=True, blank=True, verbose_name='发票代码')
    FP_HM = models.CharField(max_length=50, null=True, blank=True, verbose_name='发票号码')
    KPRQ = models.DateTimeField(null=True, blank=True, verbose_name='开票日期',db_index=True)
    YFP_DM = models.CharField(max_length=50, null=True, blank=True, verbose_name='原发票代码')
    YFP_HM = models.CharField(max_length=50, null=True, blank=True, verbose_name='原发票号码')
    YFP_KPRQ = models.DateTimeField(null=True, blank=True, verbose_name='原发票开具日期')

    KPHJJE = models.DecimalField(max_digits=18, decimal_places=2, default=0, verbose_name='价税合计金额')
    HJBHSJE = models.DecimalField(max_digits=18, decimal_places=2, default=0, verbose_name='合计不含税金额')
    HJSE = models.DecimalField(max_digits=18, decimal_places=2, default=0, verbose_name='合计税额')

    FWM = models.CharField(max_length=50, null=True, blank=True, verbose_name='校验码')

    redinv_uuid = models.CharField(max_length=32, null=True, blank=True, verbose_name='红字发票请求UUID')
    getredinv_resbillno = models.CharField(max_length=50, null=True, blank=True, verbose_name='获取红字发票结果-发票编号')
    getredinv_call_content = models.TextField(null=True, blank=True, verbose_name='获取红字发票结果-请求内容')
    getredinv_res_content = models.TextField(null=True, blank=True, verbose_name='获取红字发票结果-返回内容')
    getredinv_statusmc = models.CharField(max_length=50, null=True, blank=True, verbose_name='获取红字发票结果-状态说明')

    erpCallbackStatus = models.IntegerField(default=0, verbose_name='erp回调状态',db_index=True)#0-未回调，1-已回调，2-回调失败
    erpCallbackDate = models.DateTimeField(null=True, blank=True, verbose_name='erp回调日期')
    erpCallbackMsg = models.TextField(null=True, blank=True, verbose_name='erp回调消息')

    #tdyslxdm特定要素
    class TDYSLXDM(models.TextChoices):
        PRODUCT_OIL = '01', '成品油'
        RARE_EARTH = '02', '稀土'
        CONSTRUCTION_SERVICES = '03', '建筑服务'
        CARGO_TRANSPORT = '04', '货物运输'
        REAL_ESTATE_SALE = '05', '不动产销售'
        REAL_ESTATE_LEASE = '06', '不动产租赁'
        VEHICLE_USE_TAX = '07', '代收车船使用税'
        TOLL_FEE = '08', '通行费'
        PASSENGER_TRANSPORT = '09', '旅客运输'
        MEDICAL_SERVICES = '10', '医疗服务'
        AGRICULTURAL_PRODUCTS = '11', '农产品'
        TRACTORS_AND_COMBINES = '12', '拖拉机和联合收割机'
        MOTOR_VEHICLES = '13', '机动车'
        PHOTOVOLTAIC_PURCHASE = '14', '光伏收购'
        CIGARETTE = '15', '卷烟'

    tdyslxdm = models.CharField(max_length=2, choices=TDYSLXDM.choices, null=True, blank=True, verbose_name='特定要素')
    tdys_jsonObj = models.JSONField(null=True, blank=True, verbose_name='特定要素的jsonobject')

    PDF_URL = models.TextField(null=True, blank=True, verbose_name='PDF地址')
    PDF_FILE = models.FileField(null=True, blank=True, verbose_name='PDF文件')
    OFD_URL = models.TextField(null=True, blank=True, verbose_name='OFD地址')
    OFD_FILE = models.FileField(null=True, blank=True, verbose_name='OFD文件')
    XML_URL = models.TextField(null=True, blank=True, verbose_name='XML地址')
    XML_FILE = models.FileField(null=True, blank=True, verbose_name='XML文件')

    BZ = models.TextField(null=True, blank=True, verbose_name='备注')
    machinenr = models.IntegerField(null=True, blank=True, verbose_name='分机号')

    byzd_json = models.JSONField(null=True, blank=True, verbose_name='备用字段的jsonobject')
    cezs = models.CharField(max_length=2,null=True, blank=True, verbose_name='差额征税')#Y：是 N：否
    bzs = models.CharField(max_length=2,null=True, blank=True, verbose_name='不征税')#Y：是 N：否

    class Meta:
        verbose_name = '发票信息'
        verbose_name_plural = verbose_name
        unique_together = [['FP_DM','FP_HM']]

#发票明细
class InvoiceItem(BaseModel):
    invoice = models.ForeignKey(Invoice, on_delete=models.CASCADE, verbose_name='所属发票',db_index=True)
    xh = models.IntegerField(verbose_name='序号',db_index=True)
    yp_xh = models.CharField(max_length=10, verbose_name='原发票序号',null=True, blank=True)
    
    spfwjc = models.CharField(max_length=30, verbose_name='商品服务简称',default='')
    XMMC = models.CharField(max_length=100, verbose_name='项目名称')
    XMDW = models.CharField(max_length=30, verbose_name='项目单位')
    GGXH = models.CharField(max_length=150, verbose_name='规格型号')
    SPBM = models.CharField(max_length=19, verbose_name='商品税收编码')
    ZXBM = models.CharField(max_length=50, verbose_name='内部编码')

    HSBZ = models.IntegerField(verbose_name='含税标识')#含税标志，表示项目单价和项目金额是否含税。0 表示都不含税，1 表示都含税。
    XMSL = models.DecimalField(max_digits=18, decimal_places=2, verbose_name='项目数量')
    XMDJ = models.DecimalField(max_digits=18, decimal_places=8, verbose_name='项目单价(不含税)')
    XMJE = models.DecimalField(max_digits=18, decimal_places=2, verbose_name='项目金额(不含税)')
    XMJE_HS = models.DecimalField(max_digits=18, decimal_places=2, verbose_name='项目金额(含税)')
    XMDJ_HS = models.DecimalField(max_digits=18, decimal_places=8, verbose_name='项目单价(含税)')

    SL = models.DecimalField(max_digits=18, decimal_places=2, verbose_name='税率')

    SE = models.DecimalField(max_digits=18, decimal_places=2, verbose_name='税额')

    FPHXZ = models.CharField(max_length=2, verbose_name='发票行性质',default='0')#0正常行1折扣行2被折扣行
    YHZCBS = models.CharField(max_length=2, verbose_name='优惠政策标识',default='')#优惠政策标识 0：不使用，1：使用
    LSLBS = models.CharField(max_length=2, verbose_name='零税率标识',default='')#零税率标识 空：非零税率， 1：免税，2：不征税，
    ZZSTSGL = models.CharField(max_length=2, verbose_name='增值税特殊管理',default='')#增值税特殊管理  当YHZCBS为1时必填

    
    coustom_jsonObj = models.JSONField(null=True, blank=True, verbose_name='自定义字段的jsonobject')

    class JZJTDM(models.TextChoices):
        SOFTWARE = '01', '增值税即征即退-软件产品'
        RESOURCE_USE = '02', '增值税即征即退-资源综合利用产品'
        PIPELINE_TRANSPORT = '03', '增值税即征即退-管道运输服务'
        TANGIBLE_ASSET_LEASING = '04', '增值税即征即退-有形动产融资租赁服务'
        TANGIBLE_ASSET_SALE = '05', '增值税即征即退-有形动产融资性售后回租服务'
        NEW_MATERIALS = '06', '增值税即征即退-新型墙体材料'
        WIND_POWER = '07', '增值税即征即退-风力发电产品'
        PHOTOVOLTAIC = '08', '增值税即征即退-光伏发电产品'
        ANIMATION_SOFTWARE = '09', '增值税即征即退-动漫软件产品'
        AIRCRAFT_MAINTENANCE = '10', '增值税即征即退-飞机维修劳务'
        GOLD = '11', '增值税即征即退-黄金'
        PLATINUM = '12', '增值税即征即退-铂金'

#电票批量预赋码信息
class BatchAssignCode(BaseModel):
    app = models.ForeignKey(App, on_delete=models.CASCADE, verbose_name='所属应用',db_index=True)
    saleInvoiceConfig = models.ForeignKey(SaleInvoiceConfig, on_delete=models.CASCADE, verbose_name='销项发票配置')
    nsrsbh = models.CharField(max_length=20, verbose_name='纳税人识别号',db_index=True)
    get_datatime = models.DateTimeField(verbose_name='获取时间',db_index=True)
    fpqshm = models.CharField(max_length=20, verbose_name='发票起始号码')
    fpzzhm = models.CharField(max_length=20, verbose_name='发票终止号码')
    is_useup = models.BooleanField(default=False, verbose_name='是否用完')
    class Meta:
        verbose_name = '发票批量预赋码信息'
        verbose_name_plural = verbose_name
        ordering = ['get_datatime']

#乐企最后一张发票号码
class LeqLastFpNum(BaseModel):
    app = models.ForeignKey(App, on_delete=models.CASCADE, verbose_name='所属应用',db_index=True)
    saleInvoiceConfig = models.ForeignKey(SaleInvoiceConfig, on_delete=models.CASCADE, verbose_name='销项发票配置')
    nsrsbh = models.CharField(max_length=20, verbose_name='纳税人识别号',db_index=True)
    last_fp_num = models.CharField(max_length=20, verbose_name='最后一张发票号码')
    max_fp_num = models.CharField(max_length=20, verbose_name='最大发票号码')
    batchAssignCode = models.ForeignKey(BatchAssignCode, on_delete=models.CASCADE, verbose_name='批量预赋码信息',db_index=True)
    class Meta:
        verbose_name = '乐企最后一张发票号码'
        verbose_name_plural = verbose_name

#保证乐企同时只有一张发票在处理中
class LeqOnlyOneFp(BaseModel):
    app = models.ForeignKey(App, on_delete=models.CASCADE, verbose_name='所属应用',db_index=True)
    saleInvoiceConfig = models.ForeignKey(SaleInvoiceConfig, on_delete=models.CASCADE, verbose_name='销项发票配置')
    nsrsbh = models.CharField(max_length=20, verbose_name='纳税人识别号',db_index=True)
    count = models.IntegerField(verbose_name='发票数量')

